﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02创建图形
{
    public class LineExam
    {   
        [CommandMethod("LineDemo")]
        public void LineDemo()
        {
            #region
            ////声明一个直线对象
            //Line line1 = new Line();
            ////声明两个坐标点对象
            //Point3d startPoint = new Point3d(100, 100, 0);
            //Point3d endPoint = new Point3d(200, 200, 0);
            ////设置属性
            //line1.StartPoint = startPoint;
            //line1.EndPoint = endPoint;
            ////声明图形数据库对象
            //Document doc = Application.DocumentManager.MdiActiveDocument;
            //Database db = doc.Database;
            ////开启事务处理
            //using(Transaction trans = db.TransactionManager.StartTransaction()){
            //    //打开块表
            //    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
            //    //打开块表记录
            //    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
            //    //加直线到块表记录
            //    btr.AppendEntity(line1);
            //    //更新数据
            //    trans.AddNewlyCreatedDBObject(line1, true);
            //    //事务提交
            //    trans.Commit();
            //}
            //Line line2 = new Line(new Point3d(200, 200, 0), new Point3d(200, 300, 0));
            //Line line3 = new Line(new Point3d(200, 300, 0), new Point3d(200, 100, 0));
            //Line line4 = new Line(new Point3d(200, 100, 0), new Point3d(100, 100, 0));
            //using (Transaction trans = db.TransactionManager.StartTransaction())
            //{
            //    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
            //    //打开块表记录
            //    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
            //    //加直线到块表记录
            //    btr.AppendEntity(line2);
            //    btr.AppendEntity(line3);
            //    btr.AppendEntity(line4);
            //    //更新数据
            //    trans.AddNewlyCreatedDBObject(line2, true);
            //    trans.AddNewlyCreatedDBObject(line3, true);
            //    trans.AddNewlyCreatedDBObject(line4, true);
            //    //事务提交
            //    trans.Commit();
            //}
            #endregion

            Database db = HostApplicationServices.WorkingDatabase;
            //Line line1 = new Line(new Point3d(100, 100, 0), new Point3d(200, 100, 0));


            //Line line2 = new Line(new Point3d(200, 100, 0), new Point3d(200, 200, 0));
            //Line line3 = new Line(new Point3d(200, 200, 0), new Point3d(100, 100, 0));
            //db.AddEnityToModelSpace(line1, line2, line3);
            db.AddLineToModelSpace(new Point3d(100, 100, 0), new Point3d(200, 100, 0));
            db.AddLineToModelSpace(new Point3d(200, 200, 0), 200, 60);
        }
        
    }
}
